package tms.model.time;

import javax.persistence.*;
import java.util.Date;

@Entity
@Table(name = "TIME")
@NamedQueries({
        @NamedQuery(name = "TimeEntity.getForPeriod", query = "select t from TimeEntity t where t.startTime > :firstDate and t.startTime < :secondDate and t.userId = :userId"),
        @NamedQuery(name = "TimeEntity.updateRunning", query = "update TimeEntity t set t.runTime = t.runTime + :triggerPeriod where t.isRunning = true"),
        @NamedQuery(name = "TimeEntity.stopRunningForUser", query = "update TimeEntity t set t.isRunning = false, t.stopTime = :stopDate where t.userId = :userId and (t.isRunning = true or t.stopTime is null)"),
        @NamedQuery(name = "TimeEntity.pauseForUser", query = "update TimeEntity t set t.isRunning = false where t.userId = :userId"),
        @NamedQuery(name = "TimeEntity.timesNumberBeforeDate", query = "select count(t) from TimeEntity t where t.userId = :userId and t.startTime < :date"),
        @NamedQuery(name = "TimeEntity.timesNumberAfterDate", query = "select count(t) from TimeEntity t where t.userId = :userId and t.startTime > :date"),
        @NamedQuery(name = "TimeEntity.getLastStartDate", query = "select max(t.startTime) from TimeEntity t where t.userId = :userId"),
        @NamedQuery(name = "TimeEntity.autostop", query = "select max(t.startTime) from TimeEntity t where t.userId = :userId")        
})
public class TimeEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "ID")
    private Integer id;

    @Column(name = "USER_ID", nullable = false)
    private Integer userId;

    @Column(name = "START_TIME", nullable = false)
    @Temporal(TemporalType.TIMESTAMP)
    private Date startTime;

    @Column(name = "STOP_TIME")
    @Temporal(TemporalType.TIMESTAMP)
    private Date stopTime;

    @Column(name = "RUN_TIME")
    private Long runTime = 0L;

    @Column(name = "RUNNING_FLAG")
    private Boolean isRunning = Boolean.FALSE;

    @Column(name = "USER_NOTIFIED_FLAG")
    private Boolean isUserNotified = Boolean.FALSE;


    public TimeEntity() {
    }

    public TimeEntity(Integer id) {
        this.id = id;
    }



    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    public Date getStartTime() {
        return startTime;
    }

    public void setStartTime(Date startTime) {
        this.startTime = startTime;
    }

    public Date getStopTime() {
        return stopTime;
    }

    public void setStopTime(Date stopTime) {
        this.stopTime = stopTime;
    }

    public Long getRunTime() {
        return runTime;
    }

    public void setRunTime(Long runTime) {
        this.runTime = runTime;
    }

    public Boolean getRunning() {
        return isRunning;
    }

    public void setRunning(Boolean running) {
        isRunning = running;
    }

    public Boolean getUserNotified() {
        return isUserNotified;
    }

    public void setUserNotified(Boolean userNotified) {
        isUserNotified = userNotified;
    }

    public boolean isPaused() {
        return (stopTime == null && !isRunning);
    }
}
